package com.aqie.rabbit.repositories;

import com.aqie.rabbit.bean.db.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, String> {

    Optional<User> findByPhone(String phone);

    Optional<User> findByName(String name);

    Optional<User> findByPasswordAndPhone(String password, String phone);


    Optional<User> findByToken(String token);

    // follower 是 targetId 关注者 f.originId f1.targetId and u.followers.targetId=?1
    // "select u from User u join fetch u.followers where u.followers.targetId=?1
    @Query(value = "select u from User u join fetch u.following as f where f.originId=?1")
    Optional<User> getFollowing(String id);

    @Query(value = "select u from User u join fetch u.followers as f where f.targetId=?1")
    Optional<User> getFollower(String id);

    @Query(value = "select count(*) from  tb_user_follow as f where f.origin_id=?1",
            nativeQuery = true)
    Optional<Integer> getFollowingNum(String id);

    @Query(value = "select u.id,u.name,u.password,u.pushId,u.token from User u where u.id<>?1 and u.pushId=?2")
    List<User> findUserListByPushId(@Param("id") String id, @Param("pushId") String pushId);

    // 从懒加载中查询用户 加载关注的人列表
    @Query(value = "select u from User u join fetch u.following as f where f.originId=?1")
    Optional<User> contact(String id);

    // 模糊搜索用户 且不等于自己  信息必须完善
    @Query(value="select u from User u where  u.name like?1 and id<>?2 and u.portrait is not null and u.description is not null")
    Optional<List<User>> findByNameComplete(String name,String id);
}
